<?php

namespace app\store\controller\server;

use app\store\controller\Controller;
use app\store\model\appointment\Service as ServiceModel;
use app\store\model\Express as ExpressModel;
use think\Db;

/**
 * 分销订单
 * Class Order
 * @package app\store\controller\apps\dealer
 */
class Order extends Controller
{
    /**
     * 服务订单列表
     * @param null $user_id
     * @param int $is_settled
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($user_id = null, $is_settled = -1, $start_time = null, $end_time = null, $search = null, $phone = null)
    {
        $model = new ServiceModel;
        $list = $model->lists($user_id, $is_settled, $start_time, $end_time, $search, $phone);
        foreach($list as $k=>$v){

            $list[$k]['service_prices'] = intval($v['service_price'])*intval($v['server_num']);

            $endtime = strtotime($v['end_time']);
            $starttime = strtotime($v['start_time']);
            if($endtime > $starttime){
                $hastime = $this->timediff($starttime,$endtime);
                $list[$k]['use_time'] = $hastime['hour']."时".$hastime['min']."分";
            }


            $server_user = Db::name("server_user")->where(array("user_id"=>$v['service_id']))->find();
            $level = $server_user['level'];

            $goods = Db::name("goods")->where(array("goods_id"=>$v['user_goods_id']))->find();
            if($goods['level_set']){
                $levelset = json_decode($goods['level_set'],true);
                foreach($levelset as $key=>$val){
                    if($val['id'] == $level){
                        $service_price = $val['commission'] ? $val['commission'] : 1;
                        if($v['type_id'] == 7 || $v['type_id'] == 14){
                            $service = Db::name("appointment_service")->where(array("order_id"=>$v['order_id']))->find();
                            $list[$k]['service_prices'] = $service_price*$service['use_time'];
                        }else{
                            $list[$k]['service_prices'] = $service_price;
                        }
                    }
                }
            }
        }

        return $this->fetch('index', compact('list'));
    }
    function timediff($begin_time,$end_time)
    {
        if ( $begin_time < $end_time ) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        $timediff = $endtime - $starttime;
        $days = intval( $timediff / 86400 );
        $remain = $timediff % 86400;
        $hours = intval( $remain / 3600 );
        $remain = $remain % 3600;
        $mins = intval( $remain / 60 );
        $secs = $remain % 60;
        $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs );
        return $res;
    }
    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function export($dataType)
    {
        $ServiceModel = new ServiceModel();
        return $ServiceModel->exportList($dataType, $this->request->param());
    }

    public function submit()
    {
        $model = new ServiceModel;
        if ($model->edit($this->postData('service'))) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /*
     * 获取累计时长
     */
    public function sum_money()
    {
        $start = input('start_time');
        $user_id = input('user_id');
        $over = input('over_time');
        $start = strtotime($start);
        $over = strtotime($over);
        if(!$over){
            $over = time();
        }
        if (!$user_id){
            $res = [
                'code'=>0,
                'msg'=>'user_id必传'
            ];
            return json($res);
        }

        $list = ServiceModel::where(['service_id'=>$user_id,'status'=>1])->where('create_time','between',[$start,$over])->select();
        $sum_time = 0;
        foreach ($list as $val){
//            $sum_time += $val['end_time'] - $val['start_time'];
            $sum_time += $val['use_time'];
        }
//        $sum_time = sprintf("%.0f",$sum_time / 60);

        $res = [
            'code'=>1,
            'msg'=>'请求成功',
            'data'=>$sum_time
        ];

        return json($res);

    }
}